<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddMaintenance" type="primary" style="float:right;margin-top:13px;">录入维修车辆
    </el-button>
  </div>

  <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      style="width:1000px;padding:40px;"
      :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input readonly :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择" v-model="maintenanceArr.license">
              <el-option v-for="item in licenseArr " :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="maintenanceArr.item">
              <el-option v-for="item in itemArr " :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="maintenanceArr.addr">
              <el-option v-for="item in placeArr " :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="maintenanceArr.material">
              <el-option v-for="item in materialArr " :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" v-model="maintenanceArr.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                            v-model="maintenanceArr.repairTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="maintenanceArr.status">
              <el-radio :label="1">维修中</el-radio>
              <el-radio :label="2">已完成</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveMaintenance">录入</el-button>
    </template>
  </el-dialog>

  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input
            placeholder="请输入内容"
            style="width:220px;"
            v-model="searchMaintenanceForm.license"
            @keydown.enter="loadMaintenance"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input
            placeholder="请输入内容"
            style="width:220px;"
            v-model="searchMaintenanceForm.username"
            @keydown.enter="loadMaintenance"></el-input>
      </el-form-item>
      <el-form-item label="维修状态">
        <el-input
            placeholder="请输入内容"
            style="width:220px;"
            v-model="searchMaintenanceForm.status"
            @keydown.enter="loadMaintenance"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadMaintenance">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card style="margin:20px;">
    <el-table :data="maintenanceList">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="license" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="itemTypeFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="addrTypeFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialTypeFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="statusTypeFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editMaintenance(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteMaintenance(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false);
const dialogTitle = ref('录入维修车辆');
const user = ref(getUser());
const maintenanceArr = ref({
  username: user.value.username,
  license: '',
  item: '',
  addr: '',
  material: '',
  fee: '',
  repairTime: '',
  status: ''
})
const maintenanceList = ref([])
const searchMaintenanceForm = ref({
  license: '',
  username: '',
  status: ''
})
onMounted(() => {
  loadMaintenance();
  loadDictOptions(itemArr, 'maintenance_item');
  loadDictOptions(placeArr, 'maintenance_place');
  loadDictOptions(materialArr, 'maintenance_materials');
  loadDictOptions(statusArr, 'maintenance_status')
})
const statusTypeFormatter = (row, column, cellValue, index) => {
  if (!statusArr.value) {
    return cellValue;
  }
  for (let item of statusArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const itemTypeFormatter = (row, column, cellValue, index) => {
  if (!itemArr.value) {
    return cellValue;
  }
  for (let item of itemArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const addrTypeFormatter = (row, column, cellValue, index) => {
  if (!placeArr.value) {
    return cellValue;
  }
  for (let item of placeArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const materialTypeFormatter = (row, column, cellValue, index) => {
  if (!materialArr.value) {
    return cellValue;
  }
  for (let item of materialArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const licenseArr = ref([])
const itemArr = ref([])
const placeArr = ref([])
const materialArr = ref([])
const statusArr = ref([])
const loadLicense = () => {
  axios.get(BASE_URL + '/v1/vehicle/selectAll').then((response) => {
    if (response.data.code == 2000) {
      licenseArr.value = response.data.data.map(item => ({
        label: item.license,
        value: item.license
      }));
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
const beforeAddMaintenance = () => {
  dialogVisible.value = true;
  dialogTitle.value = '录入维修车辆';
  loadLicense();
}
const saveMaintenance = () => {
  let data = qs.stringify(maintenanceArr.value);
  axios.post(BASE_URL + '/v1/maintenance/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('添加成功');
      dialogVisible.value = false;
      loadMaintenance();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
const loadMaintenance = () => {
  let data = qs.stringify(searchMaintenanceForm.value);
  axios.get(BASE_URL + '/v1/maintenance/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      maintenanceList.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
const editMaintenance = (id) => {
  dialogTitle.value = '编辑维修车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/maintenance/select?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      maintenanceArr.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
const deleteMaintenance = (id) => {
  axios.post(BASE_URL + '/v1/maintenance/delete/' + id).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('删除成功');
      loadMaintenance();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
const handleClose = () => {
  if (confirm('确认要关闭本窗口吗?')) {
    dialogVisible.value = false;
    maintenanceArr.value = {};
  }
}
const resetSearch = () => {
  searchMaintenanceForm.value = {};
  loadMaintenance();
}
</script>

<style scoped>

</style>